package com.example.elmspringboot.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.elmspringboot.controller.utils.Result;
import com.example.elmspringboot.domain.Address;
import com.example.elmspringboot.service.AddressService;
import com.example.elmspringboot.service.Impl.AddressServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Tag(name = "AddressController", description = "地址管理")
@RestController
@RequestMapping("/api/address")
public class AddressController {
    @Resource
    private AddressService addressService;

    @Operation(summary = "通过userid获取地址",description = "需要登录后访问")
    @GetMapping
    public Result getAddress(@RequestParam Integer userId){
       return new Result(true, addressService.getAddressByUserId(userId),"success");
    }
    @Operation(summary = "删除地址",description = "需要登录后访问")
    @DeleteMapping
    public Result deleteAddress(@RequestParam Integer addressId){
        return new Result(false,addressService.removeById(addressId),"fail");
    }
    @Operation(summary = "更新地址",description = "需要登录后访问")
    @PutMapping
    public Result updateAddress(@RequestBody Address address){
        boolean flag = addressService.updateById(address);
        if(flag){
            return new Result(true,addressService.getById(address.getId()),"success");
        }
        return new Result(false,null,"fail");
    }
    @Operation(summary = "新增地址",description = "需要登录后访问")
    @PostMapping("/add-address")
    public Result addAddress(@RequestBody Address address){
        Boolean flag = addressService.save(address);
        return new Result(flag,null,"success");
    }

    @GetMapping("/get-address")
    public Result getAddressById(@RequestParam Integer id){
        return new Result(true, addressService.getById(id));
    }


}
